package com.yntsoft.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springside.modules.cache.memcached.SpyMemcachedClient;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Encodes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yntsoft.entity.Role;
import com.yntsoft.entity.User;
import com.yntsoft.mapper.RoleMapper;
import com.yntsoft.mapper.UserMapper;

@Service
public class UserService {
	
	private static final int SALT_SIZE = 8;//盐的大小
	private static final int hashIterations=1;//散列次数  默认一次  与配置文件中保持一致
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;

	public User queryUserByUserName(String userName) {
		return userMapper.queryUserByUserName(userName);
	}

	/**
	 * 查询用户列表
	 * @param searchParam
	 * @return
	 */
	public PageInfo<User> queryUserList(Map<String, Object> searchParam,Integer pageSize) {
		Integer currentpage = 1;
		if(null != searchParam.get("currentpage") && !searchParam.get("currentpage").equals("")) {
			currentpage = Integer.parseInt(searchParam.get("currentpage").toString());
		}
		PageHelper.startPage(currentpage,pageSize);
		List<User> userList = userMapper.queryUserList(searchParam);
		for(int i = 0 ; i < userList.size() ; i++ ){
			User user = userList.get(i);
			List<Role> roles = roleMapper.queryByUserId(user.getId());//通过userId  获取用户拥有的角色
			String roleStr = "";
			for(int j =0 ;j < roles.size() ; j ++){
				Role role = roles.get(j);
				if("".equals(roleStr)){
					roleStr = role.getRoleName();
				}else{
					roleStr += "," + role.getRoleName();
				}
			}
			user.setRoles(roleStr);
		}
		PageInfo<User> pageInfo = new PageInfo<User>(userList);
		return pageInfo;
	}

	/**
	 * 保存用户
	 * @param user
	 * @return
	 */
	public Integer saveUser(User user) {
		if(user.getId() == null){
			user.setPassword("132456");
			return this.insertSelective(user);
		}else{
			if(user.getPassword()!= null&& user.getPassword()!="" ){
				byte[] salt = Digests.generateSalt(SALT_SIZE);
				String saltStr = Encodes.encodeHex(salt);
				Md5Hash md5Hash = new Md5Hash(user.getPassword(), saltStr, hashIterations);
				String password_md5 =  md5Hash.toString();
				user.setSalt(Encodes.encodeHex(salt));
				user.setPassword(password_md5);
			}
			return userMapper.updateByPrimaryKeySelective(user);
		}
		
	}

	/**
	 * 添加用户同时对用户的密码进行加密
	 * @param user
	 * @return
	 */
	private Integer insertSelective(User user) {
		//加密 用户密码
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		String saltStr = Encodes.encodeHex(salt);
		Md5Hash md5Hash = new Md5Hash(user.getPassword(), saltStr, hashIterations);
		String password_md5 =  md5Hash.toString();
		user.setSalt(Encodes.encodeHex(salt));
		user.setPassword(password_md5);
		return userMapper.insertSelective(user);
	}

	public User getUserById(Integer userId) {
		return userMapper.getUserById(userId);
	}

	/**
	 * 批量生产用户
	 * @param ids
	 * @return
	 */
	public Integer userDel(String ids) {
		String[] realId = ids.split(",");
		List<String> idList = new ArrayList<>();
		for(int i = 0 ;i < realId.length ;i++ ){
			idList.add(realId[i]);
		}
		return userMapper.userDel(idList);
	}
	
}
